package com.systop.busafter.user.controller;

import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.User;
import com.systop.busafter.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //修改
    @RequestMapping(value="/updatauser",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatauser(HttpServletRequest request, @RequestParam(value = "user_personal",required = false) MultipartFile user_personal,@RequestParam(value = "user_head",required = false) MultipartFile user_head,@RequestParam(value = "user_name") String user_name,@RequestParam(value = "user_card") String user_card,@RequestParam(value = "user_name_n") String user_name_n,@RequestParam(value = "user_password") String user_password,@RequestParam(value = "user_other") String user_other,@RequestParam(value = "user_number") String user_number,@RequestParam(value = "user_remarks") String user_remarks,@RequestParam(value = "user_s") String user_s,@RequestParam(value = "user_id") String user_id) throws IOException {
        User user = new User();
        user.setUser_name(user_name);
        int user_id1 = Integer.parseInt(user_id);
        user.setUser_id(user_id1);
        user.setUser_personal(Io.img(user_personal,request));
        user.setUser_card(user_card);
        user.setUser_head(Io.img(user_head,request));
        user.setUser_name_n(user_name_n);
        String user_password1 = Io.encode(user_password.getBytes());
        user.setUser_password(user_password1);
        user.setUser_other(user_other);
        user.setUser_number(user_number);
        user.setUser_remarks(user_remarks);
        user.setUser_s(user_s);
        int i =  userService.updatauser(user);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }

    //添加注册(账号，密码，手机号))
    @RequestMapping(value="/zcinsertuser",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult zcinsertuser(@RequestBody User user){
        String encode = Io.encode(user.getUser_password().getBytes());

        user.setUser_password(encode);

        int i =  userService.zcinsertuser(user);
        if (i == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", i);
        }
    }

    //账号登陆
    @RequestMapping(value="/userLogin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userLogin(@RequestBody User user){
        String encode = Io.encode(user.getUser_password().getBytes());
        user.setUser_password(encode);
        User user1  =  userService.userLogin(user);
        if (user1 == null) {
            return ResponseResult.build(500, "登陆失败");
        } else {
            return ResponseResult.build(200, "登陆成功", user1);
        }
    }
    //手机登陆
    @RequestMapping(value="/numuserLogin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult numuserLogin(@RequestBody User user){
        User user1  =  userService.userLogin(user);
        if (user1 == null) {
            return ResponseResult.build(500, "登陆失败");
        } else {
            return ResponseResult.build(200, "登陆成功", user1);
        }
    }

    //查看全部
    @RequestMapping(value="/userListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userListall(){
        List<User> list =  userService.userListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id查
    @RequestMapping(value="/userIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userIdlist(@RequestParam(value = "user_id")int user_id){
        List<User> list =  userService.userIdlist(user_id);

        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //删除
    @RequestMapping(value="/deleteuser",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deleteuser(@RequestParam(value = "user_id")int user_id){
       int i =  userService.deleteuser(user_id);
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }

    //通过名字查
    @RequestMapping(value="/userNamelist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userNamelist(@RequestParam(value = "user_name")String user_name){
        List<User> list =  userService.userNamelist(user_name);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过账号查
    @RequestMapping(value="/userZhlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userZhlist(@RequestParam(value = "user_name_n")String user_name_n){
        List<User> list =  userService.userZhlist(user_name_n);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过身份证号查
    @RequestMapping(value="/usercard",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult usercard(@RequestParam(value = "user_card")String user_card){
        List<User> list =  userService.usercard(user_card);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过手机号查(暂时有问题)
    @RequestMapping(value="/userNumlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult userNumlist(@RequestParam(value = "user_number")String user_number){
        List<User> list =  userService.userNumlist(user_number);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

}
